/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
//package oop.demo.restaurantmanagementsystem;
package RestaurantManagementSystem;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * Restaurant billing implementation that handles 
 * customer orders and bill calculation
 * @author ashongtical
 * @author Anka
 */

public class RestaurantBilling extends RestaurantBillingSystem {
    private ArrayList<Meal> orderList;
    private HashMap<String, Integer> mealQuantities;

    //Constructor to initialize order list and meal quantities map
    public RestaurantBilling() {
        super();
        orderList = new ArrayList<>();
        mealQuantities = new HashMap<>();
    }

    //Add a meal from the menu to the customer's order
    //mealName: The name of the meal to add
    //return true if meal was added successfully, false otherwise
    public Boolean addMealToOrder(String mealName) {
        Meal meal = findMealByName(mealName);
        if (meal != null) {
            orderList.add(meal);
            mealQuantities.put(mealName, mealQuantities.getOrDefault(mealName, 0) + 1);
            return true;
        } else {
            System.out.println("Meal '" + mealName + "' not found on the menu.");
        }
        return false;
    }

    //Override the calculateBill() and calculate the total bill for the customer's order
    //return the total bill amount
    @Override
    public double calculateBill() {
        double totalBill = 0;
        for (Meal meal : orderList) {
            int quantity = mealQuantities.get(meal.getName());
            totalBill += meal.getPrice() * quantity;
        }
        return totalBill;
    }
    
    //Clear the current order
    public void clearOrder() {
        orderList.clear();
        mealQuantities.clear();
    }

    //Display the current order with meal quantities and total price
    public String displayOrder() {
        if (orderList.size() == 0) {
            System.out.println("The order is currently empty.");
            return "The order is currently empty.";
        } else {
            String orderString = "Current Order:\n";
            for (Meal meal : orderList) {
                int quantity = mealQuantities.get(meal.getName());
                orderString += meal.getName() + " × " + quantity + " = " + meal.getPrice() * quantity +"\n";
            }
            orderString += "Total Price: " + calculateBill() + "\n";
            System.out.println(orderString);
            return orderString;
        }
    }

    //Generate a formatted bill for the customer
    //customerName: The name of the customer
    //return String representation of the bill
    public String generateBill(String customerName) {
        String billString = "";
        billString += "Bill for " + customerName + ":\n" + displayOrder() + "\n";
        return billString;
    }
}
